package rhinestone.TUI;

import rhinestone.Model.Lease;
import rhinestone.Control.LeasingController;
import rhinestone.Model.Customer;
import rhinestone.Control.CustomerController;
import rhinestone.Model.Machinery;
import rhinestone.Vlad_GUI.NewJFrame;

/**
 * This class is responsible for leasing UI.
 * It extends abstract UI class is called by MainMenuUI.
 *
 * @author Group 2
 * @version 2010.12.15
 */
public class LeaseUI extends UI {
	private static final String ID_MESSAGE = "Lease ID: ";
	private static final String PERDIOD_MESSAGE = "Period: ";
	private LeasingController leasingController;
	private CustomerController customerController;
	private Customer customer;

	/**
	 * Constructor which takes the controller as an argument.
	 * @param controller A controller it should use.
	 */
	public LeaseUI(LeasingController leasingController, CustomerController customerController) {
		this.leasingController = leasingController;
		this.customerController = customerController;
	}

	/**
	 * This method holds the logic of the UI.
	 * Depending on the users input, it calls particular method of controller.
	 */
	public void startMenu() {
		boolean customerFound = promptCustomerLogin();
		boolean finished = false;
		while(!finished && customerFound) {
			printMenu();
			int choice = promptChoiceInput();
			switch(choice) {
				case 1:
					scanMachineryChoice();
					break;
				case 2:
					checkoutLeaseChoice();
					finished = true;
					break;
				case 3:
					removeMachineryChoice();
					break;
				case 4:
					printCurrentLeaseChoice();
					break;
				case 5:
					searchLeaseChoice();
					break;
				case 6:
					cancelLeaseChoice();
					finished = true;
					break;
				default:
					System.err.println("ERR: LeasingUI default option");
					break;
			}
		}
	}

	private boolean promptCustomerLogin() {
		String cpr = promptStringInput(CPR_MESSAGE);
		customer = customerController.getCustomer(cpr);
		if(customer != null) {
			leasingController.initializeLease(NewJFrame.LOGGED_IN, customer);
			return true;
		}
		System.err.println("Customer with CPR " + cpr + " NOT found.");
		return false;
	}

	/**
	 * The menu the user sees when he gets here.
	 */
	protected void printMenu() {
		System.out.println(" ----- LEASING MENU -----");
		System.out.println("\t(-1-) Scan machinery");
		System.out.println("\t(-2-) Checkout lease");
		System.out.println("\t(-3-) Remove machinery");
		System.out.println("\t(-4-) Print current lease");
		System.out.println("\t(-5-) Search lease");
		System.out.println("\t(-6-) Cancel lease");
	}

	/**
	 * Down there are a methods which are called by startMenu method
	 * depending on the users choice
	 */
	private void scanMachineryChoice() {
		String barcode = promptStringInput(BARCODE_MESSAGE);
		int period = promptIntInput(PERDIOD_MESSAGE);
		leasingController.addMachinery(barcode, period);
	}

	private void checkoutLeaseChoice() {
		leasingController.checkoutLease();
	}

	private void removeMachineryChoice() {
		String barcode = promptStringInput(BARCODE_MESSAGE);
		leasingController.removeMachinery(barcode);
		System.out.println("Machinery removed.");

	}

	private void cancelLeaseChoice() {
		leasingController.cancelLease();
	}

	private void printCurrentLeaseChoice() {
		Lease lease = leasingController.getCurrentLease();
		printLease(lease);
	}

	private void printLease(Lease lease) {
		System.out.println(lease);
	}

	private void searchLeaseChoice() {
		int id = promptIntInput(ID_MESSAGE);
		Lease lease = leasingController.getLease(id);
		if(lease != null)
			printLease(lease);
		else
			System.err.println("Lease with ID " + id + " does NOT exists.");
	}
}
